<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;

/**
 * 业绩统计
 *
 * @icon fa fa-circle-o
 */
class Tongji extends Backend
{

    /**
     * Tongji模型对象
     * @var \app\admin\model\user\Tongji
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\user\Tongji;
        $this->view->assign("isTeacherList", $this->model->getIsTeacherList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
//            if ($this->request->request('keyField')) {
//                return $this->selectpage();
//            }
            $soushuo = json_decode(input('get.filter'),true);

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if(!empty($soushuo['username'])){
                $list = $this->model
                    ->where(['is_teacher'=>1])
                    ->where(['nickname'=>$soushuo['username']])
                    ->order('distribution_income', 'desc')
                    ->paginate($limit);
            }else{
                $list = $this->model
                    ->where(['is_teacher'=>1])
                    ->order('distribution_income', 'desc')
                    ->paginate($limit);
            }


            foreach ($list as $row) {
                $row->visible(['id','username','nickname','mobile','avatar','createtime']);
            }
            $arr = $list->toArray();
            $str_time = strtotime(date('Y-m-d 00:00:00'));
            $end_time = strtotime(date('Y-m-d 23:59:59'));
            if(input('get.filter')){
                $soushuo = json_decode(input('get.filter'),true);
                if(!empty( $soushuo['createtime'])){
                    $time_arr = explode(' - ', $soushuo['createtime']);
                    $str_time = strtotime($time_arr[0]);
                    $end_time = strtotime($time_arr[1]);
                }

            }
            foreach ($arr['data'] as $k=>&$v){
                $v['money_a'] = db('exam_member_order')->where('parent_id',$v['id'])->where('status',1)->where('member_config_id',1)->where('pay_time', 'between time',[$str_time,$end_time])->sum('amount');
                $v['money_b'] = db('exam_member_order')->where('status',1)->where('parent_id',$v['id'])->where('member_config_id',2)->where('pay_time', 'between time',[$str_time,$end_time])->sum('amount');
                $v['tca_num'] = db('exam_member_order')->where('status',1)->where('parent_id',$v['id'])->where('member_config_id',1)->where('pay_time', 'between time',[$str_time,$end_time])->count();
                $v['tcb_num'] = db('exam_member_order')->where('status',1)->where('parent_id',$v['id'])->where('member_config_id',2)->where('pay_time', 'between time',[$str_time,$end_time])->count();
                $v['money_all'] = $v['money_a']+$v['money_b'];
                $v['num_all'] = $v['tca_num']+$v['tcb_num'];
            }
            $ages = array_column($arr['data'], 'money_all');

            array_multisort($ages, SORT_DESC, $arr['data']);
            $result = array("total" => $list->total(), "rows" => $arr['data']);

            return json($result);
        }
        return $this->view->fetch();
    }

}
